package com.ukao.cashregister.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.HomeAdapter;
import com.ukao.cashregister.base.BaseFragment;
import com.ukao.cashregister.consts.GlobalConsts;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.listener.OnItemClickListener;
import com.ukao.cashregister.ui.arriveShop.ArriveShopFragment;
import com.ukao.cashregister.ui.arriveShop.ArriveShopHomeFragment;
import com.ukao.cashregister.ui.clothinQuery.ClothingManageFragment;
import com.ukao.cashregister.ui.createOrders.CreateOrdersFragment;
import com.ukao.cashregister.ui.orderQuiry.OrderManageFragment;
import com.ukao.cashregister.ui.packPackag.packPackagFragment;
import com.ukao.cashregister.ui.sendFactory.FactoryTabFragment;
import com.ukao.cashregister.ui.setting.SetTingFragment;
import com.ukao.cashregister.ui.statistics.StatisticsTabFragment;
import com.ukao.cashregister.ui.stokin.StorageFragment;
import com.ukao.cashregister.ui.vipManager.VipManagerFragment;
import com.ukao.cashregister.utils.ImageUtils;
import com.ukao.cashregister.widget.ShapedImageView;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ukaodev on 2017/5/17.
 */

public class HomeFragment extends BaseFragment {

    @BindView(R.id.fragment_home_recycler)
    RecyclerView recycler;
    @BindView(R.id.fragment_home_top_logo)
    ShapedImageView topLogo;
    @BindView(R.id.fragment_home_top_name)
    TextView topName;
    @BindView(R.id.fragment_home_top_info)
    TextView topInfo;
    private Unbinder unbinder;
    //菜单名字
    private List<String> list;

    private HomeAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        list = Arrays.asList(getActivity().getResources().getStringArray(R.array.home_item));
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 4) {
            @Override
            public boolean canScrollVertically() {
                //判断item数量小于16不允许滑动
                if (list.size() > 16) {
                    return super.canScrollVertically();
                } else {
                    return false;
                }
            }
        };
        recycler.setLayoutManager(gridLayoutManager);
        mAdapter = new HomeAdapter(getActivity(), list);
        recycler.setAdapter(mAdapter);
        initListener();
        //设置商户信息
        topName.setText(SaveParamets.getMercInfo());
        topInfo.setText(TextUtils.isEmpty(SaveParamets.getMercMainInfo()) ? "" : SaveParamets.getMercMainInfo());
        ImageUtils.loadImage(getActivity(), SaveParamets.getMercLogoPath(), topLogo, R.drawable.home_merc_logo);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initView();
    }


    @Override
    protected void initListener() {
        super.initListener();
        mAdapter.setOnClickListener(mOnItemClickListener);

    }

    private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
        @Override
        public void onSureItemClick(Object item, int pos) {
            switch (pos) {
                case 0:  //下单
                    start(CreateOrdersFragment.newInstance(GlobalConsts.CREATE_ORDERS, "下单", "手机号/会员卡号"));
                    break;
                case 1:  //取衣
                    start(CreateOrdersFragment.newInstance(GlobalConsts.PICK_CLOTHING,"取衣","手机号/会员卡号/订单号"));
                    break;
                case 2:
//                    start(ArriveShopFragment.newInstance(false));
                    start(ArriveShopHomeFragment.newInstance());
                    break;
                case 3:  //送厂
                    start(FactoryTabFragment.newInstance(GlobalConsts.CREATE_ORDERS, getString(R.string.place_order)));
                    break;
                case 4:  //会员管理
                    start(VipManagerFragment.newInstance());
                    break;
                case 5:   //订单管理
                    start(OrderManageFragment.newInstance("", ""));
                    break;
                case 6:  //衣物管理
                    start(ClothingManageFragment.newInstance(getString(R.string.clothing_query), getString(R.string.clothing_code)));
                    break;
                case 7:  //设置
                    start(SetTingFragment.newInstance(0, "下单"));
                    break;
                case 8:  //入库
                    start(StorageFragment.newInstance(""));
                    break;
//                case 9:
//                    start(ArriveShopFragment.newInstance(true));
//                    break;
                case 9:
                    start(packPackagFragment.newInstance("", ""));
                    break;
                case 10:   //营收分析
                    start(StatisticsTabFragment.newInstance("",""));
                    break;
            }
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void startWifiFrament() {

        start(SetTingFragment.newInstance(1, ""), SINGLETASK);

    }


}
